package top.appx.service.impl;

import org.springframework.stereotype.Service;
import top.appx.dao.FoodDao;
import top.appx.dao.OrderDao;
import top.appx.entity.FoodEntity;
import top.appx.entity.OrderEntity;
import top.appx.qo.OrderQO;
import top.appx.service.OrderService;
import top.appx.util.DateTimeUtil;
import top.appx.vo.FoodVO;
import top.appx.vo.OrderVO;
import top.appx.vo.Pager;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by john on 2015/7/12.
 */
@Service
public class OrderServiceImpl implements OrderService{
    @Resource
    private OrderDao orderDao;

    @Resource
    private FoodDao foodDao;
    public int save(OrderEntity order) {
        order.setCreateTime(DateTimeUtil.now());
        orderDao.save(order);
        return order.getId();
    }

    public void delById(int id) {
        OrderEntity order = orderDao.getById(OrderEntity.class,id);
        orderDao.delete(order);
    }

    public Pager<OrderVO> queryByPage(OrderQO bean) {
        Pager<OrderEntity> pager = orderDao.queryByPage(bean,bean.getPage(),bean.getPageSize());
        Pager<OrderVO> pagerVO = new Pager<OrderVO>();
        List<OrderVO> list = new ArrayList<OrderVO>();
        for (OrderEntity a:pager.getRows()){
            OrderVO ov = new OrderVO(a);
            list.add(ov);
        }
        pagerVO.setRows(list);
        pagerVO.setTotal(pager.getTotal());
        return pagerVO;
    }

    public OrderVO queryById(int id) {
        OrderEntity order = orderDao.getById(OrderEntity.class, id);
        if(order!=null){
            return new OrderVO(order);
        }
        return null;
    }

    public List<OrderVO> queryUseOrder() {
        OrderQO oq = new OrderQO();
        return null;
    }

    public OrderEntity queryByIp(String ip) {
        return orderDao.queryByIp(ip);
    }

    public void changeStatus(OrderEntity order) {
        OrderEntity entity = orderDao.getById(OrderEntity.class,order.getId());
        entity.setStatus(order.getStatus());
        orderDao.update(entity);
    }

    public List<OrderVO> allDealding() {
        OrderQO qo = new OrderQO();
        qo.setStatus(1);
        Pager<OrderEntity> pager = orderDao.queryByPage(qo, 1, 100);
        List<OrderVO> list = new ArrayList<OrderVO>();
        for (OrderEntity a:pager.getRows()){
            OrderVO ov = new OrderVO(a);
            int foodId = ov.getFood();
            FoodEntity food = foodDao.getById(FoodEntity.class,foodId);
            ov.setFoodVO(new FoodVO(food));
            list.add(ov);
        }
        return list;
    }
}
